$import("com.joypard.platform.security.role.RoleDI");

/**
 * 角色管理-角色列表视图
 */
K.defView("com.joypard.platform.security.role.RoleGridPanelView", {
	roleCtrl : null,
	roleView : null,

	purviewConfig : {
		addRole : "platform.security.role.addRole",
		updateRole : "platform.security.role.updateRole",
		delRole : "platform.security.role.delRole"
	},

	RoleGridPanelView : function(roleView) {
		this.roleView = roleView;
		this.rightMenu = this.createRoleGridPanelRightMenu();

		E.addListener("addRoleSuccessEvent", this.onAddRoleSuccessEvent, this);
		E.addListener("updateRoleSuccessEvent", this.onUpdateRoleSuccessEvent, this);
		E.addListener("delRolesSuccessEvent", this.onDelRoleSuccessEvent, this);
	},

	getRoleGridPanel : function() {
		if (this.roleGridPanel == null) {
			this.roleGridPanel = this.createRoleGridPanel();
			this.loadRoles();
		}
		return this.roleGridPanel;
	},

	/**
	 * 创建角色列表面板
	 */
	createRoleGridPanel : function() {
		var me = this;
		var sm = new Ext.grid.CheckboxSelectionModel({
			listeners : {
				rowselect : function(sm, rowIndex, r) {
					me.roleView.userGridPanel.getStore().load({
						params : {
							'roleId' : r.get('id')
						}
					});
				}
			}
		});
		var options = {
			title : "角色列表",
			iconCls : "p-tab",
			region : 'center',
			viewConfig : {
				forceFit : true
			},
			tbar : [ {
				text : "新增",
				hidden : !RoleGridPanelView.purviewAddRole(),
				iconCls : "add",
				handler : K.createDelegate(this.onAddRole, this)
			}, {
				text : "修改",
				hidden : !RoleGridPanelView.purviewUpdateRole(),
				iconCls : 'update',
				handler : K.createDelegate(this.onUpdateRole, this)
			}, {
				text : "删除",
				hidden : !RoleGridPanelView.purviewDelRole(),
				iconCls : 'delete',
				handler : K.createDelegate(this.onDelRoles, this)
			}, {
				text : "查询",
				iconCls : 'search',
				handler : K.createDelegate(this.loadRoles, this)
			} ],
			store : RoleDI.createRoleStore(),
			sm : sm,
			cm : new Ext.grid.ColumnModel([ sm,// 复选框列
			{
				id : 'code',
				header : "角色编码",
				sortable : true,
				locked : false,
				dataIndex : 'code'
			}, {
				header : "角色名称",
				sortable : true,
				dataIndex : 'name'
			}, {
				header : "是否启用",
				renderer : function(v, c, r) {
					return r.get("enable") == 1 ? "是" : "否";
				}
			}, {
				header : "创建时间",
				sortable : true,
				dataIndex : 'createTime',
				renderer : function(value) {
					return ((value instanceof Date) ? (new Date(value).format("Y-m-d")) : value);
				}
			} ]),
			bbar : {
				xtype : "pagination"
			},
			listeners : {
				rowcontextmenu : function(g, rowIndex, e) {
					g.getSelectionModel().selectRow(rowIndex, e.shiftKey);
					me.rightMenu.showAt(e.getXY());
					e.stopEvent();
				}
			}
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 创建角色面板右键菜单
	 */
	createRoleGridPanelRightMenu : function() {
		var me = this;
		var roleGridPanelRightMenu = new Ext.menu.Menu({
			items : [ {
				text : '修改角色',
				handler : function() {
					var record = me.rolePanel.getSelectionModel().getSelected();
					if (record.id) {
						me.getEditRoleWindow().setTitle("修改角色");
						me.getEditRoleWindow().show();
						me.getEditRoleWindow().load({
							url : RoleDI.urls.URL_updataRole + '?id=' + record.id,
							scripts : true
						});
					}
				}
			}, {
				text : "删除角色",
				handler : function() {
					var record = me.rolePanel.getSelectionModel().getSelected();
					if (record.id && confirm("你确定要删除该角色吗？")) {
						E.fireEvent("delRolesEvent", record.id);
					}
				}
			} ]
		});
		return roleGridPanelRightMenu;
	},

	/**
	 * 点击新增按钮触发事件
	 */
	onAddRole : function() {
		var appSystemId = this.roleView.orgTreePanel.getSelectionModel().getSelectedNode()['id'];
		if (appSystemId) {
			this.getEditRoleWindow().setTitle("新增角色");
			this.getEditRoleWindow().show();
			this.getEditRoleWindow().load({
				url : RoleDI.urls.URL_addRole + "?appSystemId=" + appSystemId,
				scripts : true
			});
		}
	},

	/**
	 * 显示编辑角色窗口,包括新增和修改
	 */
	getEditRoleWindow : function() {
		if (this.editRoleWindow == null) {
			this.editRoleWindow = new K.Ext.Window({
				title : "角色编辑",
				iconCls : "p-edit",
				width : 600,
				height : 380,
				closeAction : "hide",
				buttons : [ {
					text : '确定',
					iconCls : "w-save",
					handler : K.createDelegate(this.onSaveRole, this)
				}, {
					text : '取消',
					iconCls : "w-cancel",
					handler : K.createDelegate(function() {
						this.getEditRoleWindow().hide();
					}, this)
				} ]
			});
		}
		return this.editRoleWindow;
	},

	/**
	 * 保存. 新增/修改通过判断当前form的state属性
	 */
	onSaveRole : function() {
		var $form = $("#roleForm", this.getEditRoleWindow().getEl().dom);
		var state = $form.attr("state"), eventName = null;
		if (state == K.state.update) {
			eventName = "updateRoleEvent";
		} else if (state == K.state.add) {
			eventName = "addRoleEvent";
		}
		if (eventName) {
			E.fireEvent(eventName, $form[0]);
		}
	},
	/**
	 * 新增角色成功提示
	 */
	onAddRoleSuccessEvent : function(output) {
		this.getEditRoleWindow().hide();
		alert("设置值");
		K.msg("新增角色【" + output.getParameter("role").name + "】成功", this.getRoleGridPanel().tbar);
	},

	/**
	 * 点击修改按钮触发事件
	 */
	onUpdateRole : function() {
		var rec = this.getRoleGridPanel().getSelectionModel().getSelected();
		if (rec) {
			var id = rec.get('id');
			this.getEditRoleWindow().setTitle("修改角色");
			this.getEditRoleWindow().show();
			this.getEditRoleWindow().load({
				url : RoleDI.urls.URL_updataRole + '?id=' + id,
				scripts : true
			});
		} else {
			K.err("请选择一个需要修改的角色", this.getRoleGridPanel().tbar);
		}
	},

	/**
	 * 更新角色成功提示
	 */
	onUpdateRoleSuccessEvent : function(output) {
		this.getEditRoleWindow().hide();
		var role = output.getParameter("role");
		var record = this.getRoleGridPanel().getStore().getById(role.id);
		K.Ext.flyRecord(record).set(role).commit();
		K.msg("修改角色【" + output.getParameter("role").name + "】成功", this.getRoleGridPanel().tbar);
	},

	/**
	 * 删除角色信息
	 */
	onDelRoles : function() {
		if (this.getRoleGridPanel().getSelectionModel().hasSelection()) {
			if (confirm("你确定要删除角色吗？")) {
				var records = this.getRoleGridPanel().getSelectionModel().getSelections();
				var ids = new Array();
				for ( var i = 0; i < records.length; i++) {
					ids[i] = records[i].data.id;
				}
				E.fireEvent("delRolesEvent", ids.join(","));
			}
		} else {
			K.err('请选择要删除的角色', this.getRoleGridPanel().tbar);
		}
	},

	/**
	 * 删除成功提示
	 */
	onDelRoleSuccessEvent : function() {
		var records = this.getRoleGridPanel().getSelectionModel().getSelections();
		this.getRoleGridPanel().getStore().remove(records);
		E.fireEvent("清空用户列表");
		K.msg('删除角色成功!', this.getRoleGridPanel().tbar);
	},

	loadRoles : function() {
		this.getRoleGridPanel().getStore().load({
			params : K.serializeJson(this.roleView.getForm())
		});
	}
});
